package com.oanda.blackberry.util;

import java.util.Vector;

import com.oanda.blackberry.restapi.Account;
import com.oanda.blackberry.restapi.AccountStatus;
import com.oanda.blackberry.restapi.OrderListResponse;
import com.oanda.blackberry.restapi.OrderPollResponse;
import com.oanda.blackberry.restapi.PositionsListResponse;
import com.oanda.blackberry.restapi.Quote;
import com.oanda.blackberry.restapi.Rate;
import com.oanda.blackberry.restapi.TradeListResponse;
import com.oanda.blackberry.restapi.TradePollResponse;
import com.oanda.blackberry.restapi.User;

public abstract class AbstractController implements PropertyChangeListener {
    private Vector registeredViews;
    private Vector registeredModels;

    public AbstractController() {
        this.registeredViews = new Vector();
        this.registeredModels = new Vector();
    }

    public void addModel(Model model) {
        this.registeredModels.addElement(model);
        model.addPropertyChangeListener(this);
    }

    public void removeModel(Model model) {
        this.registeredModels.removeElement(model);
        model.removePropertyChangeListener(this);
    }

    public void addView(AbstractViewPanel view) {
        this.registeredViews.addElement(view);
    }

    public void removeView(AbstractViewPanel view) {
        this.registeredViews.removeElement(view);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        for (int i = 0; i < this.registeredViews.size(); i++) {
            AbstractViewPanel view = (AbstractViewPanel) this.registeredViews.elementAt(i);
            view.modelPropertyChange(evt);
        }
    }

    protected void setModelProperty(String propertyName, Object newValue) {
        if (propertyName.equals("setHistoryChartTypeIndex")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setHistoryChartTypeIndex(((Integer) newValue).intValue());
                }
            }
        } else if (propertyName.equals("setHistoryTimeInterval")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setHistoryTimeInterval((String) newValue);
                }
            }
        } else if (propertyName.equals("setOrderPollResponse")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setOrderPollResponse((OrderPollResponse) newValue);
                }
            }
        } else if (propertyName.equals("setTradePollResponse")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setTradePollResponse((TradePollResponse) newValue);
                }
            }
        } else if (propertyName.equals("setOrderList")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    if (newValue == null)
                        object.setOrderList(null);
                    else
                        object.setOrderList((OrderListResponse[]) (OrderListResponse[]) newValue);
                }
            }
        } else if (propertyName.equals("setPositionsList")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setPositionList((PositionsListResponse[]) (PositionsListResponse[]) newValue);
                }
            }
        } else if (propertyName.equals("setTradeList")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    if (newValue == null) {
                        object.setTradeList(null);
                    } else
                        object.setTradeList((TradeListResponse[]) (TradeListResponse[]) newValue);
                }
            }
        } else if (propertyName.equals("setRateHistoryUpdate")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setRateHistoryUpdate((Rate) newValue);
                }
            }
        } else if (propertyName.equals("setRateHistory")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setRateHistory((Rate) newValue);
                }
            }
        } else if (propertyName.equals("newSellTradeOpen")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setNewTradeOpenSell((Quote) newValue);
                }
            }
        } else if (propertyName.equals("newBuyTradeOpen")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setNewTradeOpenBuy((Quote) newValue);
                }
            }
        } else if (propertyName.equals("setRateQuote")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setRateQuotes((Rate) newValue);
                }
            }
        } else if (propertyName.equals("setRateQuoteAll")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setRateQuotesAll((Rate) newValue);
                }
            }
        } else if (propertyName.equals("endToEndRequired")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setEndToEndRequired(((Boolean) newValue).booleanValue());
                }
            }
        } else if (propertyName.equals("enableHttpsRedirect")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setEnableHttpsRedirect(((Boolean) newValue).booleanValue());
                }
            }
        } else if (propertyName.equals("trustAll")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setTrustAll(((Boolean) newValue).booleanValue());
                }
            }
        } else if (propertyName.equals("notUseSymbol")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setInactiveSymbol((String) newValue);
                }
            }
        } else if (propertyName.equals("useSymbol")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setActiveSymbol((String) newValue);
                }
            }
        } else if (propertyName.equals("setConnectionSuffixIndex")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setConnectionSuffixIndex(((Integer) newValue).intValue());
                }
            }
        } else if (propertyName.equals("setRateSymbol")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setRateSybmol((Rate) newValue);
                }
            }
        } else if (propertyName.equals("setUserSettings")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setUserSettings((UserSettings) newValue);
                }
            }
        } else if (propertyName.equals("setUrlIndex")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setSelectedPlatformIndex(((Integer) newValue).intValue());
                }
            }
        } else if (propertyName.equals("useAccount")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setActiveAccount((String) newValue);
                }
            }
        } else if (propertyName.equals("setAccountStatus")) {
            for (int i = 0; i < this.registeredModels.size(); i++)
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setAccountStatus((AccountStatus) newValue);
                }
        } else if (propertyName.equals("setStatusMessage")) {
            for (int i = 0; i < this.registeredModels.size(); i++) {
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setStatusMessage((String) newValue);
                }
            }
        } else if (propertyName.equals("sessionToken")) {
            for (int i = 0; i < this.registeredModels.size(); i++)
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setSessionToken((String) newValue);
                }
        } else if (propertyName.equals("isStatusShown")) {
            for (int i = 0; i < this.registeredModels.size(); i++)
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setIsStatusShown(((Boolean) newValue).booleanValue());
                }
        } else if (propertyName.equals("setAccount")) {
            for (int i = 0; i < this.registeredModels.size(); i++)
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setAccount((Account) newValue);
                }
        } else if (propertyName.equals("setUser"))
            for (int i = 0; i < this.registeredModels.size(); i++)
                if ((this.registeredModels.elementAt(i) instanceof Model)) {
                    Model object = (Model) this.registeredModels.elementAt(i);
                    object.setUser((User) newValue);
                }
    }
}
